package keeto.com.vn.classified.ui;

import keeto.com.vn.classified.network.IProtocol;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.Toast;
public class InUIThreadHandler extends Handler {

	public static final int WHAT_HANDLE_RETURNCODE = 1;
	
	/**
	 * You should be very careful to use this method, do not
	 * 
	 * @param returnCode
	 *            - the return code
	 * @param softToast
	 *            - if <code>true</code> the handler will only show a toast to
	 *            tell user the error message. <br/>
	 *            if <code>false</code> the handler will perform some default
	 *            behavior. <br/>
	 *            you should be very careful to use this method. if you are not
	 *            sure what is the default behavior, just pass a
	 *            <code>true</code> value to show message, and handle the
	 *            exception condition in following code.
	 */
	private void handleReturnCode(final Activity activity, final int returnCode,
			final boolean softToast) {
		activity.runOnUiThread(new Runnable(){

			@Override
			public void run() {
				if (softToast) {

					String message = getMessageFromReturnCode(returnCode, activity.getResources());
					if (!TextUtils.isEmpty(message)) {
						showToast(message, true, activity);
					}

				} else {

					String message = getMessageFromReturnCode(returnCode, activity.getResources());

					switch (returnCode) {
					case IProtocol.STATUS_CODE_OK_200:

						break;
					case IProtocol.STATUS_CODE_AUTHENTICATION_FAIL_403:
					case IProtocol.STATUS_CODE_ACCOUNT_NOT_EXIST_616:
						// go to login activity
						showToast(message, true, activity);
//						LoginActivity.actionLauch(activity);
						break;

					case IProtocol.STATUS_CODE_ROSTER_NOT_AUTHORIZED_TO_CHECK_STATUS_404:

						// TODO not sure what to do, when this status happens
						showToast(message, false, activity);
						break;

					case IProtocol.STATUS_CODE_SESSIOON_TIMEOUT_408:

						// the auth key is out of date, in this case should ask user to
						// login again
						showToast(message, true, activity);
						showLogonAlertDialog(activity);

						break;
					case IProtocol.STATUS_CODE_SESSION_DUPLICATE_409:
						showReLogonDialog(activity);
						break;
					case IProtocol.STATUS_CODE_GENERICFAIL_500:

						// do not know the case
						showToast(message, true, activity);

						break;

					case IProtocol.STATUS_CODE_ROSTER_ALREADY_EXIST_420:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_ROSTER_NOT_EXIST_424:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_NETWORKFAIL_501:
					case IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502:
						showToast(message, true, activity);
						ShowSetNetworkDialog(activity);
						break;

					case IProtocol.STATUS_CODE_SOCKECT_EXCEPTION_504:
						showToast(message, true, activity);
						break;
					case IProtocol.STATUS_CODE_UNSUPPORTED_ENCODING_EXCEPTION_505:
						showToast(message, true, activity);
						break;
					case IProtocol.STATUS_CODE_CLIENT_PROTOCOL_EXCEPTION_506:
						showToast(message, true, activity);
						break;
					case IProtocol.STATUS_CODE_IO_EXCEPTION_507:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_REGISTER_USER_EXSIT_600:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_USER_NOT_FOUND_601:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_OBJECT_NOT_FOUND_602:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_LIKE_EXIST_603:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_CANT_ADD_SELF_611:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_SMS_AUTH_FAILED_703:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_SMS_AUTH_TIME_OUT_708:
						showToast(message, true, activity);
						break;

					case IProtocol.STATUS_CODE_SUBSCRIPTION_EXPIRY_706:
						showResubscribeDialog(activity);
						break;

					case IProtocol.STATUS_CODE_SERVICE_SUSPENDED_707:
						showSuspendAlertDialog(activity);
						break;

					default:
						return;
					}

				}
			}
			
		});


	}

	private void showSuspendAlertDialog(final Activity activity) {
		// TODO Auto-generated method stub
		activity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(activity);
				builder.setMessage(R.string.msg_status_code_service_suspend)
						.setPositiveButton(R.string.text_ok,
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										activity.finish();
									}
								}).setCancelable(false).create().show();
			}
		});
	}

	private void showResubscribeDialog(final Activity activity) {

		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setMessage(activity.getResources().getString(R.string.msg_status_code_service_expiry,
				activity.getResources().getString(R.string.app_name)))
				.setPositiveButton(
						R.string.msg_status_service_expiry_resubscribe,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
//								SplashScreenActivity.actionLaunch(activity);
//								activity.finish();
							}
						}).setCancelable(false).create().show();
	}

	public String getMessageFromReturnCode(int returnCode, Resources res) {

		String message = null;

		switch (returnCode) {
		case IProtocol.STATUS_CODE_OK_200:
			message = res.getString(R.string.msg_ok);
			break;
		case IProtocol.STATUS_CODE_AUTHENTICATION_FAIL_403:
		case IProtocol.STATUS_CODE_ACCOUNT_NOT_EXIST_616:
			message = res.getString(R.string.msg_auth_failed);
			break;
		case IProtocol.STATUS_CODE_ROSTER_NOT_AUTHORIZED_TO_CHECK_STATUS_404:
			message = res.getString(R.string.msg_roster_not_authorized_to_check_status);
			break;
//		case IProtocol.STATUS_CODE_SESSIOON_TIMEOUT_408:
//			message = res.getString(R.string.msg_session_timeout);
//			break;
		case IProtocol.STATUS_CODE_GENERICFAIL_500:
			message = res.getString(R.string.msg_generic_fail);
			break;
		case IProtocol.STATUS_CODE_ROSTER_ALREADY_EXIST_420:
			message = res.getString(R.string.msg_roster_already_exist);
			break;
		case IProtocol.STATUS_CODE_ROSTER_NOT_EXIST_424:
			message = res.getString(R.string.msg_roster_not_existed);
			break;
		case IProtocol.STATUS_CODE_NETWORKFAIL_501:
			message = res.getString(R.string.msg_networkfail);
			break;
		case IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502:
			message = res.getString(R.string.msg_connection_timeout);
			break;
		case IProtocol.STATUS_CODE_SOCKECT_EXCEPTION_504:
			message = res.getString(R.string.msg_sockect_exception);
			break;
		case IProtocol.STATUS_CODE_UNSUPPORTED_ENCODING_EXCEPTION_505:
			message = res.getString(R.string.msg_status_code_unsupported_encoding_exception);
			break;
		case IProtocol.STATUS_CODE_CLIENT_PROTOCOL_EXCEPTION_506:
			message = res.getString(R.string.msg_client_protocol_exception);
			break;
		case IProtocol.STATUS_CODE_IO_EXCEPTION_507:
			message = res.getString(R.string.msg_io_exception);
			break;
		case IProtocol.STATUS_CODE_REGISTER_USER_EXSIT_600:
			message = res.getString(R.string.msg_account_exsited);
			break;
		case IProtocol.STATUS_CODE_USER_NOT_FOUND_601:
			message = res.getString(R.string.msg_user_not_found);
			break;
		case IProtocol.STATUS_CODE_OBJECT_NOT_FOUND_602:
			message = res.getString(R.string.msg_object_not_found);
			break;
		case IProtocol.STATUS_CODE_LIKE_EXIST_603:
			message = res.getString(R.string.msg_sns_like_exist);
			break;
		case IProtocol.STATUS_CODE_CANT_ADD_SELF_611:
			message = res.getString(R.string.msg_connection_cannot_add_self);
			break;
		case IProtocol.STATUS_CODE_SMS_AUTH_FAILED_703:
			message = res.getString(R.string.msg_status_code_sms_auth_failed);
			break;
		case IProtocol.STATUS_CODE_SMS_AUTH_TIME_OUT_708:
			message = res.getString(R.string.msg_status_code_sms_auth_time_out, res.getString(R.string.app_name));
			break;
		case IProtocol.STATUS_CODE_SUBSCRIPTION_EXPIRY_706:
			message = res.getString(R.string.msg_status_code_service_expiry);
			break;
		case IProtocol.STATUS_CODE_SERVICE_SUSPENDED_707:
			message = res.getString(R.string.msg_status_code_service_suspend);
			break;
		default:
			return "";
		}

		return message;

	}

	public void ShowSetNetworkDialog(final Activity activity) {

		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(R.string.session_time_out_dialog_title);
		builder.setMessage(R.string.msg_connection_timeout);
		builder.setPositiveButton(R.string.session_time_out_dialog_network,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// switch to the Network Setting UI.
						Intent intent = new Intent(
								"android.settings.WIRELESS_SETTINGS");
						activity.startActivity(intent);
					}
				});

		builder.setNegativeButton(R.string.text_cancel,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
		builder.create().show();

	}

	public void showLogonAlertDialog(final Activity activity) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity)
				.setTitle(R.string.logon_dialog_alert_title)
				.setMessage(R.string.logon_dialog_alert_message)
				.setPositiveButton(R.string.text_login,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

//								BuiltinService bs = BuiltinServiceManager
//										.getInstance(activity)
//										.getBuitinService();
//
//								IMCore.getInstance(activity).taskLogon(
//										bs.getUserName(), bs.getPassword(),
//										IConstant.ONLINE, null);

							}
						})
				.setNegativeButton(R.string.text_cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								dialog.cancel();

							}
						});
		builder.create().show();
	}

	private void showReLogonDialog(final Activity activity) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity)
				.setTitle(R.string.logon_dialog_alert_title)
				.setMessage(
						activity.getResources().getString(
								R.string.msg_session_duplicate_message,
								activity.getResources().getString(
										R.string.app_name)))
				.setPositiveButton(R.string.text_login,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
//								BuiltinService bs = BuiltinServiceManager
//										.getInstance(activity)
//										.getBuitinService();
//
//								IMCore.getInstance(activity).taskLogon(
//										bs.getUserName(), bs.getPassword(),
//										IConstant.ONLINE, null);

							}
						})
				.setNegativeButton(R.string.text_cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								dialog.cancel();

							}
						});
		builder.create().show();

	}

	/**
	 * 
	 * @param message
	 *            - toast message
	 * @param longtime
	 *            - <code>true</code> time long, <code>false</code> time short
	 */
	public void showToast(final String message, final boolean longtime,
			final Activity activity) {
		Toast.makeText(activity, message,
				longtime ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
	}

	public void takeActionByReturnCode(
			Activity activity, int returnCode, boolean b) {
		handleReturnCode(activity, returnCode, b);
	}



}